from PySide6.QtGui import QShowEvent
from PySide6.QtWidgets import * # type: ignore
from PySide6.QtCore import * # type: ignore
from PySide6.QtGui import * # type: ignore
from qfluentwidgets import *  # type: ignore

from gui.components.settings.font_card import FontSetterCard
from utils import cfg_util

class SettingsWindow(FluentWindow):
    '''Settings window for configuring application preferences'''
    
    def __init__(self):
        super().__init__()
        self.setWindowTitle('Island Settings')
        self.setFixedSize(600, 700)

        # Create scroll area for settings content
        scroll_area = ScrollArea()
        scroll_area.setObjectName('settings_widget')

        settings_widget = QWidget()
        layout = QVBoxLayout()
        
        # Font selection components
        self.font_label = QLabel(self.tr('Font'))
        self.font_picker = FontSetterCard()
        self.font_picker.setFixedHeight(350)
        
        layout.addWidget(self.font_label)
        layout.addWidget(self.font_picker)
        settings_widget.setLayout(layout)

        scroll_area.setWidget(settings_widget)
        scroll_area.setWidgetResizable(True)

        # Add settings interface to navigation
        self.addSubInterface(scroll_area, FluentIcon.SETTING, 'Island Settings')
        self.show()

        # Initialize font setting if not set
        if cfg_util.config_instance.font == 'Unknown':
            cfg_util.config_instance.font = QFont().defaultFamily()
            cfg_util.saveConfig()

    def showEvent(self, event: QShowEvent) -> None:
        '''Update font selection when window is shown'''
        self.font_picker.updateSelection()
        return super().showEvent(event)

    def closeEvent(self, e):
        '''Hide instead of closing when user clicks X'''
        e.ignore()
        self.hide()